package com.lemon.smartsafetynew.ui.fragment.record;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerNewTClickListener;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.entity.record.CompanyPeopleVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.record.CompanyPeopleAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 一企一档企业人员信息
 * Created by JiangAn on 2018/5/23.
 */

public class RecordCompanyPeopleActivity extends SwipeBackActivity implements RecyclerNewTClickListener<CompanyPeopleVo> {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_company_people_count)
    TextView tvCompanyPeopleCount;
    @Bind(R.id.rc_peopele)
    RecyclerView rcPeopele;

    private static MeCompanyVo companyVo;
    private CompanyPeopleAdapter adapter;
    private List<CompanyPeopleVo> mList;

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.company_peopel));
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_VO)) {
                companyVo = (MeCompanyVo) bundle.getSerializable(KeyConstants.KEY_COMPANY_VO);
            }
        }
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordCompanyPeopleActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_record_company_people;
    }

    @Override
    protected void initViews() {
        super.initViews();

        ivReturn.setVisibility(View.INVISIBLE);

        mList = new ArrayList<>();
        adapter = new CompanyPeopleAdapter(mList, this,true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rcPeopele.setLayoutManager(layoutManager);
        rcPeopele.setAdapter(adapter);

        getData(); //获取用户列表数据
    }

    /**
     * 获取用户列表数据
     */
    private void getData() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("pageSize", 1000);
        params.put("pageNum", 1);

        RecordServices.init().getCompanyPeopleInfo(params);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {
        if (KeyConstants.URL_GET_RECORD_COMPANY_PEOPLE_INFO.equals(event.getTag())) {                    //获取人员信息
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (adapter != null) {
                        mList.clear();
                        adapter.notifyDataSetChanged();
                        adapter.addItems((List<CompanyPeopleVo>) event.getObj());
                        tvCompanyPeopleCount.setText("" + mList.size());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });

                }
            }
        }
    }

    @Override
    public void onNewItemClick(CompanyPeopleVo data, int position) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getMobile()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
